package com.acane.instagram.mapper;

import com.acane.instagram.pojo.Post;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ReviewMapper extends BaseMapper<Post> {
    @Select("<script>" +
            "SELECT " +
            "  p.id, " +
            "  p.user_id AS userId, " +
            "  p.content, " +
            "  p.status, " +
            "  p.like_count AS likeCount, " +
            "  p.comment_count AS commentCount, " +
            "  p.created_time AS createdTime, " +
            "  p.share_range AS shareRange, " +
            "  p.show_range AS showRange, " +
            "  p.thumbnail_url AS thumbnailUrl, " +
            "  u.user_name AS 'user.userName', " +  // 修正字段名
            "  u.nick_name AS 'user.nickName', " +   // 修正字段名
            "  u.avatar AS 'user.avatar' " +
            "FROM post p " +
            "LEFT JOIN user u ON p.user_id = u.id " +
            "<where>" +
            "  <if test='userName != null and userName != \"\"'>" +
            "    AND u.user_name LIKE CONCAT('%', #{userName}, '%')" + // 使用user_name字段
            "  </if>" +
            "  <if test='content != null and content != \"\"'>" +
            "    AND p.content LIKE CONCAT('%', #{content}, '%')" +
            "  </if>" +
            "  <if test='startTime != null'>" +
            "    AND p.created_time &gt;= #{startTime}" +
            "  </if>" +
            "  <if test='endTime != null'>" +
            "    AND p.created_time &lt;= #{endTime}" +
            "  </if>" +
            "  <if test='status != null'>" +
            "    AND p.status = #{status}" +
            "  </if>" +
            "</where>" +
            "ORDER BY p.created_time DESC " +
            "LIMIT #{offset}, #{size}" +
            "</script>")
    List<Post> searchPosts(
            @Param("userName") String userName,
            @Param("content") String content,
            @Param("status") Integer status,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime,
            @Param("offset") Integer offset,
            @Param("size") Integer size
    );
}
